﻿<%@ Page Title="" Language="C#" MasterPageFile="~/master/public-regular.master" AutoEventWireup="true"
    CodeFile="address-list.aspx.cs" Inherits="myaccount_address_list" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Register Namespace="ECommerce.Library.Web" TagPrefix="ucc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderNestedHeader"
    runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphMiddleContent" runat="Server">
    <script type="text/javascript">

       var IsDefault_Renderer = function (value) {
            if (value == true)
                return "Yes";
            else
                return "No";
        }

        var CommandHandler = function (command, rowIndex, grid) {
            //showObj(grid);
            grid.getRowEditor().stopEditing();
            grid.getView().refresh();
            grid.getSelectionModel().selectRow(rowIndex);
            grid.getRowEditor().startEditing(rowIndex);
        }

        var afterEdit = function (e) {

            e.grid.stopEditing();

            // Call DirectMethod
            Polana.WebService.UpdateAddressBook(e.record.data.AddressBookId, e.record.data.UserFirstName, e.record.data.UserLastName, e.record.data.UserTelephone1, e.record.data.UserAddressLine1, e.record.data.UserAddressLine2, e.record.data.CompanyName, e.record.data.UserEmailAddress, e.record.data.IsDefault);
        };

        var addressGrid;

        var deleteAddress = function () {
            if (addressGrid != null) {
                addressGrid.getRowEditor().stopEditing();

                var s = addressGrid.getSelectionModel().getSelections();
                if (s.length > 0) {

                    if (confirm("Are you sure you want to delete these address from your addressbook?")) {

                        var ids = "";

                        for (var i = 0, r; r = s[i]; i++) {
                            ids = ids + " " + r.data.AddressBookId;
                        }

                        // Call DirectMethod
                        Polana.WebService.DeleteAddress(ids);
                    }
                }
            }
        }

        var addAddress = function () {
            window.location = "<%= SiteGlobals.BaseUrl %>" + "myaccount/address-add.aspx";
        }

        var onDocumentReady = function (grid) {
            addressGrid = grid;
        }

    </script>
    <ext:ResourceManager ID="ScriptManager1" runat="server" Theme="Gray">
        <Listeners>
            <DocumentReady Fn="onDocumentReady(#{GridPanel1})" />
        </Listeners>
    </ext:ResourceManager>
    <ext:Store ID="Store1" runat="server" RemoteSort="true" OnRefreshData="Store1_RefreshData">
        <AutoLoadParams>
            <ext:Parameter Name="sort" Value="AddressBookId" Mode="Value" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="limit" Value="10" Mode="Raw" />
        </AutoLoadParams>
        <Proxy>
            <ext:PageProxy />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="AddressBookId">
                <Fields>
                    <ext:RecordField Name="AddressBookId" />
                    <ext:RecordField Name="UserFirstName" Type="String" />
                    <ext:RecordField Name="UserLastName" Type="String" />
                     <ext:RecordField Name="UserTelephone1" Type="String" />
                     <ext:RecordField Name="UserEmailAddress" Type="String" />
                    <ext:RecordField Name="UserAddressLine1" Type="String" />
                    <ext:RecordField Name="UserAddressLine2" Type="String" />
                     <ext:RecordField Name="CompanyName" Type="String" />
                    <ext:RecordField Name="IsDefault" Type="Boolean" />
                    
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <div class="admin-function-top user-home">
        <ucc:ResponseMessage ID="uccResponseMessage" runat="server"></ucc:ResponseMessage>
        <ext:GridPanel ID="GridPanel1" runat="server" EnableViewState="true" AutoHeight="true"
            StoreID="Store1">
            <ColumnModel>
                <Columns>
                    <ext:Column Header="First Name" Width="100" DataIndex="UserFirstName">
                        <Editor>
                            <ext:TextField ID="txtUserFirstName" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Last Name" Width="100" DataIndex="UserLastName">
                        <Editor>
                           <ext:TextField ID="txtUserLastName" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Phone" Width="100" DataIndex="UserTelephone1">
                        <Editor>
                           <ext:TextField ID="txtUserTelePhone" runat="server" />
                        </Editor>
                    </ext:Column>
                     <ext:Column Header="Email" Width="100" DataIndex="UserEmailAddress">
                        <Editor>
                           <ext:TextField ID="txtUserEmailAddress" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Address1" Width="100" DataIndex="UserAddressLine1">
                        <Editor>
                           <ext:TextField ID="txtUserAddressLine1" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Address2" Width="100" DataIndex="UserAddressLine2">
                        <Editor>
                           <ext:TextField ID="txtUserAddressLine2" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Company Name" Width="100" DataIndex="CompanyName">
                    <Editor>
                        <ext:TextField ID="txtCompanyName" runat="server"></ext:TextField>
                    </Editor>
                    </ext:Column>
                     
                    <ext:Column Header="Is Default" DataIndex="IsDefault" Width="80" Align="Right">
                        <Renderer Fn="IsDefault_Renderer"  />
                        <Editor>
                            <ext:Checkbox ID="chkIsActive" runat="server" Style="text-align:right;"   />
                        </Editor>
                    </ext:Column>
                    <ext:CommandColumn Width="60">
                        <Commands>
                            <ext:GridCommand Icon="NoteEdit" CommandName="Edit" Text="Edit" />
                        </Commands>
                    </ext:CommandColumn>
                </Columns>
            </ColumnModel>
            <TopBar>
            
                <ext:Toolbar ID="ActionToolBar" runat="server">
                    <Items>
                        <ext:Button ID="btnToolbarAddAddress" runat="server" Text="Add Address" Icon="Add">
                            <Listeners>
                                <Click Handler="addAddress();" />
                            </Listeners>
                        </ext:Button>
                        <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                        <ext:Button ID="btnToolbarDelete" runat="server" Text="Delete selected" Icon="Delete">
                            <Listeners>
                                <Click Handler="deleteAddress();" />
                            </Listeners>
                        </ext:Button>
                    </Items>
                </ext:Toolbar>
            </TopBar>
            <BottomBar>
                <ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="10" DisplayInfo="true"
                    DisplayMsg="Displaying address {0} - {1} of {2}" EmptyMsg="No address to display"
                    AutoWidth="true" />
            </BottomBar>
            <LoadMask ShowMask="true" />
            <SelectionModel>
                <ext:CheckboxSelectionModel ID="CheckboxSelectionModel1" runat="server" ColumnPosition="9"
                    AllowDeselect="true" CheckOnly="true" />
            </SelectionModel>
            <Plugins>
                <ext:RowEditor ID="rowedi" runat="server" SaveText="Update" ClicksToEdit="2" ErrorSummary="false"
                    ShowTooltip="={Ext.emptyFn}">
                    <Listeners>
                        <AfterEdit Fn="afterEdit" />
                    </Listeners>
                </ext:RowEditor>
            </Plugins>
            <Listeners>
                <Command Handler="CommandHandler(command, rowIndex, this)" />
            </Listeners>
            <View>
                <ext:GridView ID="GridView1" runat="server" ForceFit="true" />
            </View>
        </ext:GridPanel>
    </div>
        <script type="text/javascript">
            Ext.override(Ext.grid.GridView, {
                getEditorParent: function () {
                    return document.body;
                }
            });
    </script>
</asp:Content>
